
Ext.define('Holo.store.Trips', {
    extend: 'Ext.data.Store',
    requires: 'Holo.model.Trip',
    model: 'Holo.model.Trip',
    
    actionAddingTrip : false,
    constructor: function(cfg) {
        var me = this;
        cfg = cfg || {};
        me.callParent([Ext.apply({}, cfg)]);
    },
    
    listeners: {
        load : function(store, records, successful, operation, eOpts){
    
	    //console.log('load store');
        },
        
        add: function(store, records,  index,  eOpts){
            store.sync();
            this.actionAddingTrip = true;
	    
        },
    
        update: function(store, record, operation,  eOpts){
            store.sync();
        },
	
	datachanged : function (store, eOpts ){
            //The currentTrip id is not correctly update when it's create, so we re-define it with this datachanged event
	    // normaly when the server add a row it will return the DB id added, this action auto update the store
	    //and fire the "datachanged event", so if we try actionAddingTrip we have can get back the id return by the server
	    //console.log(store.max('id'));
	    
	    //if the last sport add is not 0 -> we receive the id affect by the DB (the ajax is finish)
	    if((this.actionAddingTrip) && (store.last().get('id')!=0)){
		this.actionAddingTrip = false;
		GlobalData.currentTrip = store.last();
	        GlobalData.currentController.screenUpdate();
		console.log('Trip officialy added, the currentTrip is the last create');
	    }
	  
	}
	
    }
    

});